using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShopController : Singleton<ShopController>
{
    ShopModel shopModel =new ShopModel();
    ShopView shopView  = new ShopView();
    LuckyView luckyView=new LuckyView();
    List<GameObject> shopList=new List<GameObject>();
    public void Init()
    {  
        shopModel.Init();
        shopView.OnCreate();
        shopView.Init();
        luckyView.OnCreate();
        OnClick();
        OnAddListener();
    }

    private void OnAddListener()
    {
        MessCenter<int>.Instance.AddLister(MsgID.ExpendMoney, RefreshTrait);
    }

    private void RefreshTrait(int obj)
    {
        shopView.showTarit.text = MainGamePanelControlle.Instance.mainGamePanelModel.PlayerPropertyData.initJb.ToString();
    }

    bool isInit = true;
    private void OnClick()
    {
        shopView.shopBuyBtn.onClick.AddListener(() =>
        {
            if (isInit)
            {
                InitBuyShop();
                isInit = false;
            }
            PanelManager.Instance.UIBuyShopPanel.SetActive(true);
            PanelManager.Instance.UILuckyPanel.SetActive(false);
        });
        shopView.luckyBtn.onClick.AddListener(() =>
        {
            PanelManager.Instance.UIBuyShopPanel.SetActive(false);
            PanelManager.Instance.UILuckyPanel.SetActive(true);
        });
        shopView.clseBtn.onClick.AddListener(() =>
        {
            PanelManager.Instance.UIBuyShopPanel.SetActive(false);
            PanelManager.Instance.UILuckyPanel.SetActive(false);
            PanelManager.Instance.UIShopPanel.SetActive(false);
        });
        luckyView.leftFreeBtn.onClick.AddListener(() =>
        {
            MessCenter<int>.Instance.DisPath(MsgID.ExpendMoney, -UnityEngine.Random.Range(15,35));
            luckyView.leftFreeBtn.interactable = false;
        });
        luckyView.rightFreeBtn.onClick.AddListener(() =>
        {
            MessCenter<int>.Instance.DisPath(MsgID.ExpendMoney, -UnityEngine.Random.Range(35, 65));
            luckyView.rightFreeBtn.interactable = false;
        });
    }
    public bool isShow=false;
    public void InitBuyShop()
    {
        for (int i = 0;i < shopModel.GetData().Count ;i++)
        {
            GameObject item = GameObject.Instantiate(Resources.Load<GameObject>("UI/Item"),GameObject.Find("Canvas").transform.Find("UIShopPanel(Clone)/UIBuyShopPanel(Clone)/Scroll View/Viewport/Content").transform);
            shopList.Add(item);
            item.GetComponent<Item>().Init(shopModel.GetData()[i]);
        }      
    }
}
